package com.bc.web.model;

import java.util.List;

import com.bc.common.CommonEnum.NotifyStatus;
import com.bc.common.CommonEnum.OrderStatus;
import com.bc.common.PayType;
import com.bc.common.basemodel.BaseTradeOrder;
import com.bc.common.message.AppidWxgjPayReq;
import com.bc.web.config.R;
import com.fs.common.extend.FSSearchCondition;
import com.fs.common.extend.FSSqlHelper;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSIDGenerate;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class TradeOrder extends BaseTradeOrder<TradeOrder>{
	
	public static final TradeOrder dao = new TradeOrder().dao();
	
	public Page<TradeOrder> search(FSSearchCondition sc){
		int pageNumber = sc.page.getPageNum();
		int pageSize = sc.page.getNumPerPage();
		FSSqlHelper sql = new FSSqlHelper("FROM trade_order");
		sql.equals("orderStatus",sc.get("orderStatus"));
		sql.equals("merchOrderNo",sc.get("merchOrderNo"));
		sql.equals("merchCode",sc.get("merchCode"));
		sql.equals("orgCode",sc.get("orgCode"));
		sql.equals("orderNo",sc.get("orderNo"));
		sql.equals("payType",sc.get("payType"));
		sql.equals("channelCode",sc.get("channelCode"));
		sql.equals("otherMerchCode",sc.get("otherMerchCode"));
		sql.equals("isInVirtualAccount",sc.get("isInVirtualAccount"));
		String payTimeStart = sc.getStr("payTimeStart");
		String payTimeEnd = sc.getStr("payTimeEnd");
		sql.date("payTime", payTimeStart, payTimeEnd);
		String createTimeStart = sc.getStr("createTimeStart");
		String createTimeEnd = sc.getStr("createTimeEnd");
		sql.date("createTime", createTimeStart, createTimeEnd);
		sql.orderBy("createTime","desc");
		return super.paginate(pageNumber, pageSize, "select *", sql.toString(), sql.getParams());
	}
	
	
	public Page<TradeOrder> searchByBuDan(FSSearchCondition sc){
		int pageNumber = sc.page.getPageNum();
		int pageSize = sc.page.getNumPerPage();
		FSSqlHelper sql = new FSSqlHelper("FROM trade_order where orderStatus = 'TS' AND payType IN ('wechat_public','wechat_qrcode','alipay_public','alipay_qrcode') ");
		sql.equals("orderStatus",sc.get("orderStatus"));
		sql.equals("merchOrderNo",sc.get("merchOrderNo"));
		sql.equals("merchCode",sc.get("merchCode"));
		sql.equals("orgCode",sc.get("orgCode"));
		sql.equals("orderNo",sc.get("orderNo"));
		sql.equals("payType",sc.get("payType"));
		sql.equals("channelCode",sc.get("channelCode"));
		sql.equals("isInVirtualAccount",sc.get("isInVirtualAccount"));
		String payTimeStart = sc.getStr("payTimeStart");
		String payTimeEnd = sc.getStr("payTimeEnd");
		sql.date("payTime", payTimeStart, payTimeEnd);
		String createTimeStart = sc.getStr("createTimeStart");
		String createTimeEnd = sc.getStr("createTimeEnd");
		sql.date("createTime", createTimeStart, createTimeEnd);
		sql.orderBy("createTime","desc");
		return super.paginate(pageNumber, pageSize, "select *", sql.toString(), sql.getParams());
	}
	
	
	
	public Long searchSumMoney(FSSearchCondition sc) {
		FSSqlHelper sql = new FSSqlHelper("select sum(amount) FROM trade_order");
		sql.equals("orderStatus",sc.get("orderStatus"));
		sql.equals("merchOrderNo",sc.get("merchOrderNo"));
		sql.equals("merchCode",sc.get("merchCode"));
		sql.equals("orgCode",sc.get("orgCode"));
		sql.equals("orderNo",sc.get("orderNo"));
		sql.equals("payType",sc.get("payType"));
		sql.equals("channelCode",sc.get("channelCode"));
		sql.equals("otherMerchCode",sc.get("otherMerchCode"));
		String payTimeStart = sc.getStr("payTimeStart");
		String payTimeEnd = sc.getStr("payTimeEnd");
		sql.date("payTime", payTimeStart, payTimeEnd);
		String createTimeStart = sc.getStr("createTimeStart");
		String createTimeEnd = sc.getStr("createTimeEnd");
		sql.date("createTime", createTimeStart, createTimeEnd);
		return Db.use(R.BD_TRADE).queryLong(sql.toString(),sql.getParams());
	}
	
	public Page<TradeOrder> searchForOrg(FSSearchCondition sc, String orgCode) {
		sc.put("orgCode", orgCode);
		return search(sc);
	}
	
	public List<TradeOrder> searchReportForAdmin(FSSearchCondition sc){
		FSSqlHelper sql = new FSSqlHelper("select merchCode,merchOrderNo,orderNo,amount,merchFee,orderStatus,notifyStatus,payType,createTime,payTime,respCode,respMsg,otherMerchCode FROM trade_order");
		sql.equals("orderStatus",sc.get("orderStatus"));
		sql.equals("merchOrderNo",sc.get("merchOrderNo"));
		sql.equals("merchCode",sc.get("merchCode"));
		sql.equals("orgCode",sc.get("orgCode"));
		sql.equals("orderNo",sc.get("orderNo"));
		sql.equals("payType",sc.get("payType"));
		sql.equals("channelCode",sc.get("channelCode"));
		sql.equals("isInVirtualAccount",sc.get("isInVirtualAccount"));
		String payTimeStart = sc.getStr("payTimeStart");
		String payTimeEnd = sc.getStr("payTimeEnd");
		sql.date("payTime", payTimeStart, payTimeEnd);
		String createTimeStart = sc.getStr("createTimeStart");
		String createTimeEnd = sc.getStr("createTimeEnd");
		sql.date("createTime", createTimeStart, createTimeEnd);
		sql.orderBy("createTime","desc");
		return find(sql.toString(),sql.getParams());
	}
	
	public List<TradeOrder> searchReportForOrg(FSSearchCondition sc, String orgCode){
		FSSqlHelper sql = new FSSqlHelper("select merchCode,merchOrderNo,orderNo,amount,merchFee,orderStatus,notifyStatus,payType,createTime,payTime,respCode,respMsg FROM trade_order");
		sql.equals("orderStatus",sc.get("orderStatus"));
		sql.equals("merchOrderNo",sc.get("merchOrderNo"));
		sql.equals("merchCode",sc.get("merchCode"));
		sql.equals("orgCode",orgCode);
		sql.equals("orderNo",sc.get("orderNo"));
		sql.equals("payType",sc.get("payType"));
		String payTimeStart = sc.getStr("payTimeStart");
		String payTimeEnd = sc.getStr("payTimeEnd");
		sql.date("payTime", payTimeStart, payTimeEnd);
		String createTimeStart = sc.getStr("createTimeStart");
		String createTimeEnd = sc.getStr("createTimeEnd");
		sql.date("createTime", createTimeStart, createTimeEnd);
		sql.orderBy("createTime","desc");
		return find(sql.toString(),sql.getParams());
	}
	
	public Page<TradeOrder> searchForMerch(FSSearchCondition sc, String merchCode) {
		sc.put("merchCode", merchCode);
		return search(sc);
	}

	public TradeOrder selectByOrderNo(String orderNo){
		return super.findFirst("select * from trade_order where orderNo = ?",orderNo);
	}
	
	public List<TradeOrder> selectClearingData(String settleDate){
		String sql = "SELECT id,channelCode,orgCode,merchCode,orderNo,payType,merchOrderNo,amount,merchFee,payTime,settleDate FROM trade_order WHERE settleDate=? AND orderStatus=? AND payType IN (?,?,?,?,?,?,?,?,?,?) ";
		return super.find(sql, settleDate, OrderStatus.Success.getValue(),PayType.DF_COMPANY, PayType.DF_PERSONAL, PayType.QUICK_PAY_SET, PayType.UNION_PAY, PayType.QUICK_PAY_CREDIT, PayType.UNION_PAY_SUB,
				PayType.ALIPAY_PUBLIC, PayType.ALIPAY_QRCODE, PayType.WECHAT_PUBLIC, PayType.WECHAT_QRCODE);
	}
	
	public List<TradeOrder> searchReportForMerch(String merchCode,String beginDate,String endDate){
		FSSqlHelper sql = new FSSqlHelper("SELECT merchCode,merchOrderNo,orderNo,payType,amount,merchFee,balance,orderStatus,payTime FROM trade_order");
		sql.equals("merchCode",merchCode);
		sql.equals("orderStatus",OrderStatus.Success.getValue());
		sql.date("settleDate", beginDate, endDate);
		sql.orderBy("payType", "asc");
		sql.orderBy("merchOrderNo","asc");
		return super.find(sql.toString(),sql.getParams());
	}
	
	
	public List<TradeOrder> searchReportForMerch(String orgCode, String merchCode,String beginDate,String endDate){
		FSSqlHelper sql = new FSSqlHelper("SELECT merchCode,merchOrderNo,orderNo,payType,amount,merchFee,balance,orderStatus,payTime FROM trade_order");
		if( null != orgCode && 0 < orgCode.length()) {sql.equals("orgCode",orgCode);}
		if( null != merchCode && 0 < merchCode.length()) {sql.equals("merchCode",merchCode);}
		sql.equals("orderStatus",OrderStatus.Success.getValue());
		sql.date("settleDate", beginDate, endDate);
		sql.orderBy("payType", "asc");
		sql.orderBy("merchOrderNo","asc");
		return super.find(sql.toString(),sql.getParams());
	}
	
	
	/**
	 * 跨境微信公众号订单--退款订单
	 * 
	 * @param 原订单信息
	 * @param 退款原因
	 * @param payType
	 * @return
	 */
	public TradeOrder createWxgjRefOrder(TradeOrder oldOrder, String refmark) {
		TradeOrder order = new TradeOrder();
		order.setId(FSIDGenerate.getUUID());
		order.setChannelCode(oldOrder.getChannelCode());
		order.setOrgCode(oldOrder.getOrgCode());
		order.setMerchCode(oldOrder.getMerchCode());
		
		order.setOrderNo(FSIDGenerate.nextOrderNo());//新的退款订单号
		order.setOriginalOrderNo(oldOrder.getOrderNo());//原支付订单号
		order.setMerchOrderNo("RF" + oldOrder.getMerchOrderNo());//原商户订单号 补充 RF标识退款
		order.setPayType("wxgjpay_refund");//支付方式是 退款
		
		order.setOrderStatus(OrderStatus.InProcess.getValue());
		order.setAmount(0 - Integer.valueOf(oldOrder.getAmount()));
		order.setMerchFee(0);
		order.setCreateTime(FSDate.getDateTime());
		order.setNotifyUrl(null);
		order.setSettleDate(FSDate.getDate());
		order.setNotifyStatus(NotifyStatus.Wait.getValue());
		order.setIsInVirtualAccount(false);
		order.setIsD0(false);
		{
			order.setRemark(refmark);//退款原因
		}
		order.setCoinType(oldOrder.getCoinType());//交易清算币种
		order.setOtherMerchCode(oldOrder.getOtherMerchCode());//微信子商户号
		return order;
	}
	
	
}
